package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author hanshuai
 * @version 1.0
 * @description 用户注册功能
 * @date 2021/12/11 20:13
 */
@RestController
@RequestMapping("user")
@Anoymous
public class UserRegisterController {

    //导入验证码对应组件
    @Reference(timeout = 3000,retries = 2, check = false)
    IKaptchaService iKaptchaService;

    //导入 register服务
    @Reference(timeout = 10000,retries = 0, check = false)
    IRegisterService iRegisterService;

    /**
     * 实现注册功能
     *
     * @return
     */
    @RequestMapping("register")
    public ResponseData userRegister(@RequestBody Map<String, String> map, HttpServletRequest request) {
        // 获取参数
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String email = map.get("email");
        String captcha = map.get("captcha");

        //判断验证码是否正确
        //获取request 校验参数
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        //获取cookie中的uuid
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName());
        }
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        //设置 uuid
        kaptchaCodeRequest.setUuid(uuid);
        // 设置 验证码
        kaptchaCodeRequest.setCode(captcha);
        //验证 并获取结果
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        //图片对应的字符串
//        String imageCode = kaptchaCodeResponse.getImageCode();
        //如果不匹配就返回验证码错误信息
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        //向用户表中插入记录
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setUserPwd(userPwd);
        userRegisterRequest.setEmail(email);

        //获取结果
        UserRegisterResponse registerResponse = iRegisterService.register(userRegisterRequest);
        //成功
        if (SysRetCodeConstants.SUCCESS.getCode().equals(registerResponse.getCode())) {
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(registerResponse.getMsg());
    }
}
